<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\TcmAroma;
use App\Constants\TcmAromaConstant;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TcmAromaController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TcmAroma(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('class');
            $grid->column('img')->image(' ', 50);
            $grid->column('pinyin');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TcmAroma(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('class');
            $show->field('img');
            $show->field('share_img');
            $show->field('pinyin');
            $show->field('meridian');
            $show->field('taste');
            $show->field('origin_label');
            $show->field('alias');
            $show->field('medicine_help');
            $show->field('clinical');
            $show->field('history');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TcmAroma(), function (Form $form) {
            $form->tab('基本信息', function (Form $form) {
                $form->display('id');
                $form->text('name')->required();
                $form->select('class')->options(TcmAromaConstant::generateArr(TcmAromaConstant::CLASS_MAP));
                $form->image('img')->uniqueName()->autoUpload()->saveFullUrl()->required();
                $form->image('share_img')->uniqueName()->autoUpload()->saveFullUrl()->required();
                $form->text('pinyin');
                $form->multipleSelect('origin_label')
                    ->options(TcmAromaConstant::generateArr(TcmAromaConstant::ORIGIN_MAP))
                    ->customFormat(function ($v) {
                        if (!$v) return [];

                        return explode('/', $v[0]);
                    })->saving(function ($v){
                        return implode('/', $v);
                    });
            })->tab('性味功效', function (Form $form) {
                $form->array('meridian', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            })->tab('功效', function (Form $form) {
                $form->array('taste', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            })->tab('别名', function (Form $form) {
                $form->array('alias', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            })->tab('用量用法', function (Form $form) {
                $form->array('medicine_help', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            })->tab('临床配伍', function (Form $form) {
                $form->array('clinical', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            })->tab('相关论述', function (Form $form) {
                $form->array('history', function ($table) {
                    $table->text('title', '标题');
                    $table->editor('content', '内容');
                    $table->text('author', '作者');
                    $table->image('image', '图片')->uniqueName()->autoUpload()->saveFullUrl();
                })->saving(function ($v){
                    return json_encode($v, JSON_UNESCAPED_UNICODE);
                });
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
